package com.iflyings.android.util

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Build

object AppUtils {

    fun isDebug(context: Context): Boolean {
        val appInfo = context.applicationInfo
        return appInfo != null && appInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0
    }

    fun isSystemApp(context: Context): Boolean {
        val appInfo = context.applicationInfo
        return appInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0
    }

    fun getAppVersionCode(packageManager: PackageManager, packageName: String): Int {
        return try {
            packageManager.getPackageInfo(packageName, 0).versionCode
        } catch (e: PackageManager.NameNotFoundException) {
            -1
        }
    }

    fun getAppVersionName(packageManager: PackageManager, packageName: String): String {
        return try {
            packageManager.getPackageInfo(packageName, 0).versionName ?: return ""
        } catch (e: PackageManager.NameNotFoundException) {
            ""
        }
    }

    @SuppressLint("QueryPermissionsNeeded")
    fun startMainActivity(context: Context, packageName: String): Boolean {
        if (packageName.isEmpty()) {
            return true
        }
        val list = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            context.packageManager.queryIntentActivities(
                Intent(Intent.ACTION_MAIN).apply {
                    addCategory(Intent.CATEGORY_LAUNCHER)
                },
                PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_ALL.toLong())
            )
        } else {
            context.packageManager.queryIntentActivities(Intent(Intent.ACTION_MAIN).apply {
                addCategory(Intent.CATEGORY_LAUNCHER)
            }, PackageManager.MATCH_ALL)
        }.filter { it.activityInfo.packageName.startsWith(packageName) }
        if (list.isNotEmpty()) {
            val fullPackageName = list.first().activityInfo.packageName
            val intent = context.packageManager.getLaunchIntentForPackage(fullPackageName)?.apply {
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
            LogHelper.i("start activity -> $fullPackageName")
            return true
        }
        return false
    }

    fun startService(context: Context, packageName: String, serviceName: String): Boolean {
        val intent = Intent().apply {
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            component = ComponentName(packageName, serviceName)
            setPackage(packageName)
        }
        val resolveInfo = context.packageManager.resolveService(intent, 0)
        if (resolveInfo != null) {
            context.startService(intent)
            LogHelper.i("start service->${resolveInfo.serviceInfo.packageName}")
            return true
        }
        return false
    }

}
